#import "BWStitchArray.h"

@interface BWStitchArray (KVOFun)

- (void) removeObjectFromStitchesAtIndex: (unsigned) index;

- (void) insertObject: (id) object
    inStitchesAtIndex: (unsigned) index;

@end // KVOFun


@implementation BWStitchArray

- (id) init
{
    if ([super init] == nil) {
        return (nil);
    }

    stitches = [[NSMutableArray alloc] init];

    return (self);

} // init


- (void) dealloc
{
    [stitches release];
    
    [super dealloc];

} // dealloc


- (int) count
{
    return ([stitches count]);

} // count


- (NSEnumerator *) objectEnumerator
{
    return ([stitches objectEnumerator]);

} // objectEnumerator


- (int) indexOfStitch: (BWStitch *) stitch
{
    return ([stitches indexOfObject: stitch]);

} // indexOfStitch


- (BWStitch *) stitchAtIndex: (int) index
{
    return ([stitches objectAtIndex: index]);

} // stitchAtIndex


- (void) removeStitch: (BWStitch *) stitch
{
    int index;
    index = [stitches indexOfObject: stitch];

    [self removeObjectFromStitchesAtIndex: index];

} // removeStitch


- (void) insertStitch: (BWStitch *) stitch
              atIndex: (int) index
{
    [self insertObject: stitch
          inStitchesAtIndex: index];

} // insertStitch


// for document i/o that's already there

- (NSArray *) stitchesAtIndexes: (NSIndexSet *) stitchIndexes
{
    NSArray *demStitches;
    demStitches = [stitches objectsAtIndexes: stitchIndexes];

    return (demStitches);

} // stitchesAtIndexes


- (void) removeStitchesAtIndexes: (NSIndexSet *) stitchIndexes
{
    [stitches removeObjectsAtIndexes: stitchIndexes];
} // removeStitchesAtIndexes


- (void) addStitches: (NSArray *) newStitches
           atIndexes: (NSIndexSet *) stitchIndexes
{
    [stitches insertObjects: newStitches
              atIndexes: stitchIndexes];

} // addStitches


- (void) addStitchesFromArray: (NSArray *) newStitches
{
    [stitches addObjectsFromArray: newStitches];

} // addStitchesFromArray


- (void) removeLastStitches: (int) count
{
    NSRange nukeRange;
    nukeRange.location = [stitches count] - count;
    nukeRange.length = count;

    [stitches removeObjectsInRange: nukeRange];

} // removeLastStitches


- (void) removeFirstStitches: (int) count
{
    NSRange nukeRange;
    nukeRange.location = 0;
    nukeRange.length = count;

    [stitches removeObjectsInRange: nukeRange];

} // removeFirstStitches


- (void) insertStitches: (NSArray *) s
             atIndexes: (NSIndexSet *) indexes
{
    [stitches insertObjects: s
              atIndexes: indexes];

} // insertStitches



- (void) addStitchesFromArrayToBeginning: (NSArray *) newStitches
{
    [stitches replaceObjectsInRange: NSMakeRange(0, 0)
              withObjectsFromArray: newStitches];

} // addObjectsFromArrayToBeginning



// --------------------------------------------------
// KVO

- (NSArray *) stitches
{
    return (stitches);

} // stitches


- (void) insertObject: (id) object
    inStitchesAtIndex: (unsigned) index
{
    [stitches insertObject: object  atIndex: index];

} // insertObject


- (void) removeObjectFromStitchesAtIndex: (unsigned) index
{
    [stitches removeObjectAtIndex: index];

} // removeObjectFromStitchesAtIndex


- (void) setStitches: (NSArray *) s
{
    [stitches removeAllObjects];
    [stitches addObjectsFromArray: s];

} // setStitches


@end // BWStitchArray

